using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;

using Jamila2.Tools;

namespace BPCSFileDaemon
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder LogBuilder = new StringBuilder();
            int LineNumber = 1;
            DaemonConfigurator CurConfigurator = null;
            //FileAnalyzer FileDaemon = null;
            FileDaemon CurFileDaemon = null;

            LogBuilder.AppendLine((LineNumber++) + ". Daemon wake up.");

            // start to read configuration
            CurConfigurator = new DaemonConfigurator();
            CurConfigurator.ReadConfiguration();

            if (CurConfigurator.ErrorMessage.Length > 0)
            {
                LogBuilder.AppendLine((LineNumber++) + ". Error : " + CurConfigurator.ErrorMessage);
            }

            LogBuilder.AppendLine((LineNumber++) + ". File Monitor successfully configured");

            // start to create file daemon
            LogBuilder.AppendLine((LineNumber++) + ". Daemon start watching.");

            CurFileDaemon = new FileDaemon(CurConfigurator);
            if (CurFileDaemon.StartWatching())
            {
                if (CurFileDaemon.LoggedMessages != null)
                {
                    for (int x = 0; x < CurFileDaemon.LoggedMessages.Count; x++)
                    {
                        LogBuilder.AppendLine((LineNumber++) + ". " + CurFileDaemon.LoggedMessages[x].ToString());
                    }
                }

                LogBuilder.AppendLine((LineNumber++) + ". File Extract Successfully Ended");
            }
            else
            {
                if (CurFileDaemon.LoggedMessages != null)
                {
                    for (int x = 0; x < CurFileDaemon.LoggedMessages.Count; x++)
                    {
                        LogBuilder.AppendLine((LineNumber++) + ". " + CurFileDaemon.LoggedMessages[x].ToString());
                    }
                }

                LogBuilder.AppendLine((LineNumber++) + ". File Extract Failed.");

            }
            
            //Do the checking for the employee Email and AD name, if empty than drop an email to superadmin

            LogBuilder.AppendLine((LineNumber++) + ". Daemon sleep again.");

            /* The End */
            string NewFolderName = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year;
            string NewFileName = "log_" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + ".txt";

            if (!Directory.Exists(CurConfigurator.PathForLogFiles + "\\" + NewFolderName))
                Directory.CreateDirectory(CurConfigurator.PathForLogFiles + "\\" + NewFolderName);

            SwissArmy.FileCreate(CurConfigurator.PathForLogFiles + "\\" + NewFolderName + "\\" + NewFileName, LogBuilder.ToString());

            Console.WriteLine(LogBuilder.ToString());

            // clean up
            LogBuilder = null;
            CurConfigurator = null;
            Console.ReadLine();
            
        }
    }
}
